// pages/add_address/add_address.js
// 获取应用实例
const app = getApp()
var baseurl = app.baseurl.url;
const util = require('../../utils/util.js'); //路径根据自己的文件目录来
Page({
  /**
   * 页面的初始数据
   */
  data: {
    yuming: app.yuming.url,
    region: ['省', '市', '县 (区)'],
    customItem: '全部',
    user_name: '',
    phone: '',
    address: '',
    id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  iptname(e) {
    this.setData({
      user_name: e.detail.value
    })
  },
  iptphone(e) {
    if (!util.checkphone(e.detail.value)) {
      // console.log(this.data.phone);
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return false
    } else {
      this.setData({
        phone: e.detail.value
      })
    }
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  iptaddress(e) {
    this.setData({
      address: e.detail.value
    })
  },
  onLoad: function (options) {
    let id = options.id
    this.setData({
      id: id
    })
    wx.request({
      url: `${baseurl}/user.UserAddress/get_details`,
      data: {
        user_id: wx.getStorageSync('user_id'),
        id: id
      },
      success: (resen) => {
        console.log(resen.data.data);
        let res = resen.data.data
        this.data.region[0] = res.province
        this.data.region[1] = res.city
        this.data.region[2] = res.area,

          this.setData({
            user_name: res.user_name,
            phone: res.phone,
            address: res.address,
            region: this.data.region
          })
      }
    })
  },
  // 修改地址
  submit() {
    if (this.data.user_name == '') {
      wx.showToast({
        title: '请填写收货人',
        icon: 'none'
      })
      return false
    }
    // 判断手机号是否正确
    if (!util.checkphone(this.data.phone)) {
      // console.log(this.data.phone);
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return false
    }
    if (this.data.region[0] == '省' || this.data.region[1] == '市' || this.data.region[2] == '县 (区)' || this.data.region[0] == '全部' || this.data.region[1] == '全部' || this.data.region[2] == '全部') {
      wx.showToast({
        title: '请选择所在的 省 市 区',
        icon: 'none'
      })
      return false
    }
    if (this.data.address == '') {
      wx.showToast({
        title: '请填写详细地址',
        icon: 'none'
      })
      return false
    }
    else {
      wx.request({
        url: `${baseurl}/user.UserAddress/edit_address`,
        data: {
          user_id: wx.getStorageSync('user_id'),
          user_name: this.data.user_name,
          phone: this.data.phone,
          province: this.data.region[0],
          city: this.data.region[1],
          area: this.data.region[2],
          address: this.data.address,
          status: 2,
          id: this.data.id
        },
        success: (res) => {
          // console.log(res.data.code);
          if (res.data.code == 200) {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
            wx.redirectTo({
              url: '/pages/address/address',
            })
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})